﻿using System;
using System.Collections.Generic;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace kb_24_the_game
{
    internal class Obstakel : ISprite
    {
        #region variabele declaratie

        public Texture2D texture;

        public GraphicsDevice graphics { get; set; }

        public SpriteBatch spriteBatch { get; set; }

        public ContentManager content { get; set; }

        public Vector2 position { get; set; }

        private int kegelTimer = 0;
        private int valTijd;
        private Vector2 startPosition;
        private int type;
        private Vector2 blockOffsetX;
        private Vector2 blockOffsetY;
        public List<Rectangle> colliderRect;
        public int snelheid;
        private SpriteFont font;

        #endregion variabele declaratie

        public Obstakel(ContentManager content, int type, Vector2 position, int tijd)
        {
            this.content = content;
            this.type = type;
            this.position = position;
            startPosition = position;
            snelheid = 3;
            valTijd = tijd;
            colliderRect = new List<Rectangle>();
            Load();
        }

        public Obstakel(ContentManager content, int type, Vector2 position)
        {
            this.content = content;
            this.type = type;
            this.position = position;
            snelheid = 3;
            font = content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            blockOffsetX = new Vector2(50, 0);
            blockOffsetY = new Vector2(0, 50);
            colliderRect = new List<Rectangle>();
            Load();
        }

        public void Load()
        {
            if (type == 1)
            {
                texture = content.Load<Texture2D>("Level1Textures/block");
            }
            else if (type == 2)
            {
                texture = content.Load<Texture2D>("Level1Textures/block");
            }
            else if (type == 3)
            {
                texture = content.Load<Texture2D>("Level1Textures/gap");
            }
            else if (type == 4)
            {
                texture = content.Load<Texture2D>("Level1Textures/BowlingPins");
            }
            else if (type == 5)
            {
                texture = content.Load<Texture2D>("Level2Textures/StreetBench");
            }
            else if (type == 6)
            {
                texture = content.Load<Texture2D>("Level1Textures/Wallet");
            }
            else if (type == 7)
            {
                texture = content.Load<Texture2D>("Level3Textures/FallingLight");
            }

        }

        public void Update(GameTime gameTime)
        {
            kegelTimer++;
        }

        public void beweegLinks()
        {
            position += new Vector2(snelheid, 0);
        }

        public void beweegRechts()
        {
            position -= new Vector2(snelheid, 0);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (type == 1)
            {
                drawStairsLeft(spriteBatch);
            }
            else if (type == 2)
            {
                drawStairsRight(spriteBatch);
            }
            else if (type == 3)
            {
                drawGap(spriteBatch);
            }
            else if (type == 4)
            {
                drawKegel(spriteBatch);
            }
            else if (type == 5)
            {
                drawBank(spriteBatch);
            }
            else if (type == 6)
            {
                drawWallet(spriteBatch);
            }
        }

        private void drawWallet(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            spriteBatch.Draw(texture, position, Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            colliderRect.Add(rect1);
        }

        public void drawKegel(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            spriteBatch.Draw(texture, position, Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, 15, 50);
            colliderRect.Add(rect1);
        }

        public void drawGap(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            spriteBatch.Draw(texture, position, Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, 100, 200);
            colliderRect.Add(rect1);
        }

        public void drawStairsLeft(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            //linker blok
            spriteBatch.Draw(texture, position, Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, 50, 50);
            colliderRect.Add(rect1);
            //Midden rij
            spriteBatch.Draw(texture, position + blockOffsetX, Color.White);
            spriteBatch.Draw(texture, position + blockOffsetX - blockOffsetY, Color.White);
            Rectangle rect2 = new Rectangle((int)position.X + (int)blockOffsetX.X, (int)position.Y, 50, 50);
            Rectangle rect3 = new Rectangle((int)position.X + (int)blockOffsetX.X, (int)position.Y - (int)blockOffsetY.Y, 50, 50);
            colliderRect.Add(rect2);
            colliderRect.Add(rect3);
            //Rechter rij
            spriteBatch.Draw(texture, position + (blockOffsetX * 2), Color.White);
            spriteBatch.Draw(texture, position + (blockOffsetX * 2) - blockOffsetY, Color.White);
            spriteBatch.Draw(texture, position + (blockOffsetX * 2) - (blockOffsetY * 2), Color.White);
            Rectangle rect4 = new Rectangle((int)position.X + ((int)blockOffsetX.X * 2), (int)position.Y, 50, 50);
            Rectangle rect5 = new Rectangle((int)position.X + ((int)blockOffsetX.X * 2), (int)position.Y - (int)blockOffsetY.Y, 50, 50);
            Rectangle rect6 = new Rectangle((int)position.X + ((int)blockOffsetX.X * 2), (int)position.Y - ((int)blockOffsetY.Y * 2), 50, 50);
            colliderRect.Add(rect4);
            colliderRect.Add(rect5);
            colliderRect.Add(rect6);
        }

        public void drawStairsRight(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            //linker rij
            spriteBatch.Draw(texture, position, Color.White);
            spriteBatch.Draw(texture, position - blockOffsetY, Color.White);
            spriteBatch.Draw(texture, position - (blockOffsetY * 2), Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, 50, 50);
            Rectangle rect2 = new Rectangle((int)position.X, (int)position.Y - (int)blockOffsetY.Y, 50, 50);
            Rectangle rect3 = new Rectangle((int)position.X, (int)position.Y - ((int)blockOffsetY.Y * 2), 50, 50);
            colliderRect.Add(rect1);
            colliderRect.Add(rect2);
            colliderRect.Add(rect3);
            //Midden rij
            spriteBatch.Draw(texture, position + blockOffsetX, Color.White);
            spriteBatch.Draw(texture, position + blockOffsetX - blockOffsetY, Color.White);
            Rectangle rect4 = new Rectangle((int)position.X + (int)blockOffsetX.X, (int)position.Y, 50, 50);
            Rectangle rect5 = new Rectangle((int)position.X + (int)blockOffsetX.X, (int)position.Y - (int)blockOffsetY.Y, 50, 50);
            colliderRect.Add(rect4);
            colliderRect.Add(rect5);
            //rechter rij
            spriteBatch.Draw(texture, position + (blockOffsetX * 2), Color.White);
            Rectangle rect6 = new Rectangle((int)position.X + ((int)blockOffsetX.X * 2), (int)position.Y, 50, 50);
            colliderRect.Add(rect6);
        }

        public void ValKegel(int offset)
        {
            if (kegelTimer >= valTijd)
            {
                position += new Vector2(0, snelheid);
                if (position.Y >= 475)
                {
                    ResetKegel(offset);
                }
            }
        }

        public void ResetKegel(int offset)
        {
            kegelTimer = 0;
            position = new Vector2(startPosition.X + offset, 0);
        }

        public void drawBank(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            spriteBatch.Draw(texture, position, Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, 140, 70);
            colliderRect.Add(rect1);
        }
    }
}
